package com.rrqs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.rrqs.utils.DateUtils;
import com.zadtek.xmqishi.R;
import com.rrqs.activity.common.Common;

import java.util.Date;

/**
 * 我的统计
 * @author kuyukeji
 *
 */
public class MytjFragment extends FragmentActivity{

	private ImageView backid,iv_rili;
	private TextView topview, rightname; // 标题,右边的标题



	private RadioGroup radioGroup;
	private FragmentTransaction fragmentTrans;

	private MytjChildFragment fragment;	//我的统计

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_mytj);
		
		backid = (ImageView) findViewById(R.id.backid);
		backid.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		topview = (TextView) findViewById(R.id.topview);
		topview.setText("我的统计");
		rightname = (TextView) findViewById(R.id.rightname);
		rightname.setVisibility(View.GONE);

		iv_rili = (ImageView) findViewById(R.id.iv_rili);
		iv_rili.setVisibility(View.VISIBLE);
		iv_rili.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(v.getContext(), DatePickerActivity.class), 1000);
			}
		});

		radioGroup = (RadioGroup) findViewById(R.id.tab_radio);
		
		fragmentTrans = getSupportFragmentManager().beginTransaction();
		if (fragment == null) {	//默认显示第一个
			fragment = new MytjChildFragment();
            fragmentTrans.add(R.id.layout_content, fragment).commit();
            fragment.setTime("2");
        }

		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radio_all: // 昨天
					fragment = new MytjChildFragment();
					String time1 = Common.getLastDay();
					fragment.setTime("1");
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;

				case R.id.radio_today:	//今天
					fragment = new MytjChildFragment();
					String timejt = Common.getToday();
					fragment.setTime("2");
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;
				case R.id.radio_nopay: // 本月
					fragment = new MytjChildFragment();
					String time = Common.getCurrMonth();
					fragment.setTime("3");
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;
					
				case R.id.radio_payed: // 上月
					fragment = new MytjChildFragment();
					String time3 = Common.getLastMonth();
					fragment.setTime("4");
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;
				}
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			assert data != null;
			if (requestCode == 1000) {
				Date mStartDate = new Date(data.getLongExtra("date", 0));
				String date = DateUtils.convertDateToStr(mStartDate, "yyyyMMdd");
					Intent intent = new Intent();
					intent.setClass(MytjFragment.this, MytjDayFragment.class);
					intent.putExtra("date",date);
					startActivity(intent);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
